
package hotelroom;

/* import statements */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Hendry Yuwono
 * 
 * This is a JPanel class called as "Door"
 * This panel will be executed first (as default)
 * It will contains 6 buttons, each button has the same instructions to pop-up a new frame whenever the button is clicked
 * 
 * 
 */
public class Door extends JPanel {
    /* creating variables */
    private JButton r1;
    private JButton r2;
    private JButton r3;
    private JButton r4;
    private JButton r5;
    private JButton r6;
    private JLabel label;
    private JLabel label1;
    private Timer timer;
    private Image img;
    // Following statement would create object ik and ni
    InsertKey ik = new InsertKey();
    NewIcon ni = new NewIcon();
    
    
    /**
     * this is a constructor contains 10 parameters.
     */
    public Door(){
        // set default icon on every button
        Icon locked = new ImageIcon(getClass().getResource("locked.png"));
        // set default font
        Font f = new Font("serif", Font.PLAIN, 24);
        
        Toolkit kit = Toolkit.getDefaultToolkit();
        img = kit.getImage(Door.class.getResource("background.png"));
        
       
        // create buttons and label
        r1 = new JButton("Room 1");
        r1.setIcon(locked);
        r1.setFont(f);
        r1.setBackground(Color.LIGHT_GRAY);
        r1.setOpaque(true);
        r2 = new JButton("Room 2");
        r2.setIcon(locked);
        r2.setFont(f);
        r2.setBackground(Color.LIGHT_GRAY);
        r3 = new JButton("Room 3");
        r3.setIcon(locked);
        r3.setFont(f);
        r3.setBackground(Color.LIGHT_GRAY);
        r4 = new JButton("Room 4");
        r4.setIcon(locked);
        r4.setFont(f);
        r4.setBackground(Color.LIGHT_GRAY);
        r5 = new JButton("Room 5");
        r5.setIcon(locked);
        r5.setFont(f);
        r5.setBackground(Color.LIGHT_GRAY);
        r6 = new JButton("Room 6");
        r6.setIcon(locked);
        r6.setFont(f);
        r6.setBackground(Color.LIGHT_GRAY);
        
        Font customFont = new Font("Pristina", Font.PLAIN, 75);
        label = new JLabel("LexiMoore Hotel");
        label.setBorder(null);
        label.setBackground(null);
        label.setFont(customFont);
        
        label1 = new JLabel("You are at level 1");
        label1.setBorder(null);
        label1.setBackground(null);
        label1.setFont(f);
        
        // add action listener on every button
        r1.addActionListener(new HandlerClass());
        r2.addActionListener(new HandlerClass());
        r3.addActionListener(new HandlerClass());
        r4.addActionListener(new HandlerClass());
        r5.addActionListener(new HandlerClass());
        r6.addActionListener(new HandlerClass());

        
        setLayout(new GridBagLayout());
        //creating an object c
        GridBagConstraints c = new GridBagConstraints();
        
        //allocating buttons on the panel 
        c.ipady = 35;
        c.ipadx = 150;
        c.gridx = 2;
        c.gridy = 2;
        add(r1,c);
        c.gridx = 2;
        c.gridy = 0;
        add(r2,c);
        c.gridx = 1;
        c.gridy = 2;
        add(r3,c);
        c.gridx = 1;
        c.gridy = 0;
        add(r4,c);
        
        c.ipady = 100;
        c.ipadx = 380;
        c.gridx = 0;
        c.gridy = 2;
        add(r5,c);
        c.gridx = 0;
        c.gridy = 0;
        add(r6,c);
        
        c.ipady = 180;
        c.ipadx = 380;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        add(label,c);
        
        c.gridx = 1;
        c.gridy = 1;
        add(label1,c);
        
        
        //timer to allow switchable button icon works 
        timer = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
               if(ik.getStatus()==true){
                   ni.setNewIcon(ni.getButton());
                   ik.setStatus(false);
                    }
            }
        });
        timer.start();
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img,0,0,this);
    }
    
    // this class implements ActionListener which gives instructions to the buttons each time it is clicked
    class HandlerClass implements ActionListener{
        @Override
            public void actionPerformed (ActionEvent e){
                if(e.getSource() == r1){
                    ik.key();
                    ik.setCodeDB("RM1234");
                    ni.setButton(r1);
                }else if(e.getSource() == r2){
                    ik.key();
                    ik.setCodeDB("RM2345");
                    ni.setButton(r2);
                }else if(e.getSource() == r3){
                    ik.key();
                    ik.setCodeDB("RM3456");
                    ni.setButton(r3);
                }else if(e.getSource() == r4){
                    ik.key();
                    ik.setCodeDB("RM4567");
                    ni.setButton(r4);
                }else if(e.getSource() == r5){
                    ik.key();
                    ik.setCodeDB("RM5678");
                    ni.setButton(r5);
                }else if(e.getSource() == r6){
                    ik.key();
                    ik.setCodeDB("RM6789");
                    ni.setButton(r6);
                } 
        }
    }
    
    
    
}
